/*
 * Copyright (c) 2010. Dylan Schell.
 */

package nl.xs4all.dylanschell.deckprinter.transform;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import nl.xs4all.dylanschell.deckprinter.model.CardInDeck;
import nl.xs4all.dylanschell.deckprinter.model.CardInSet;
import nl.xs4all.dylanschell.deckprinter.model.DeckDescriptor;
import nl.xs4all.dylanschell.deckprinter.services.ImageService;
import nl.xs4all.dylanschell.deckprinter.services.SetService;

@Singleton
public class RemoveUnknownCards implements DeckFilter
{
    private final SetService setService;
    private final ImageService imageService;

    @Inject
    public RemoveUnknownCards(SetService setService, ImageService imageService)
    {
        this.setService = setService;
        this.imageService = imageService;
    }

    public DeckDescriptor filter(DeckDescriptor input)
    {
        DeckDescriptor result = new DeckDescriptor(input.getName());
        for (String section : input.getSections())
        {
            for (CardInDeck card : input.getCardsInSection(section))
            {
                CardInSet cis = setService.getCardInSet(card);
                if (cis != null)
                {
                    try
                    {
                        CardInDeck newCard = new CardInDeck(cis.getName(), card.getQuantity(), cis.getId());
                        result.getCardsInSection(section).add(newCard);
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
        return result;
    }
}
